﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace CrossLine
{
    class Monitor
    {
        public void monitor()
        {
            DateTime currentDate = DateTime.Now.AddMinutes(-1);
            int minute = currentDate.Minute;

            List<string> signStock = new List<string>();

            Dictionary<string, double[]> cur = curDate();
            double s = cur["sh000001"][1] / cur["sh000001"][0];
            int count = ConstDate.stocks.Count;
            for (int i = 0; i < count; i++)
            {
                double transO = trans(ConstDate.stocks[i], minute);
                double[] a = cur[ConstDate.stocks[i]];
                if (transO > 0 && a[1] > 0 && a[1] > transO && (a[1] / a[0]) > s)
                {
                    if (!ConstDate.signstocks.ContainsKey(ConstDate.stocks[i]))
                    {
                        ConstDate.signstocks.Add(ConstDate.stocks[i], true);
                        signStock.Add(ConstDate.stocks[i]);
                        LogUtil.StockLine("Stock:" + ConstDate.stocks[i] + " Price:" + a[1] + "DateTime:" + currentDate);
                    }
                }
            }

            if (signStock.Count > 0)
            {
                Beep.beep();
            }

        }

        public double trans(string stock_no, int minute)
        {
            string transHis = WebUtil.transHis(stock_no);
            //new Array('10:28:18', '5000', '9.280', 'DOWN')
            //string regexStr = @"(new\sArray\\(\'(\d{2}:\d{2}:\d{2})\',\s\'(\d*)\',\s\'(\d*\.\d*)\',";
            string regexStr = @"\(\'(\d{2}\:\d{2}\:\d{2})\',\s\'(\d*)\',\s\'(\d*\.\d*)\',";
            //string regexStr = @"(\d{4}-\d{2}-\d{2})(\d*\.\d{2})";
            Regex regex = new Regex(regexStr, RegexOptions.Singleline | RegexOptions.Compiled);
            MatchCollection mc = regex.Matches(transHis);
            int count = mc.Count;
            double fund = 0.0;
            double vol = 0.0;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    //LogUtil.LogLine(sub_stock_no + " " + date + " " + m.Groups[1] + " " + m.Groups[2]);
                    DateTime dt = DateTime.Parse(mc[i].Groups[1].ToString());
                    int m = dt.Minute;
                    if (m == minute) {
                        string volume = Convert.ToString(mc[i].Groups[2]);
                        string pri = Convert.ToString(mc[i].Groups[3]);
                        fund += (Convert.ToDouble(pri) * Convert.ToDouble(volume));
                        vol += (Convert.ToDouble(volume));
                        LogUtil.LogLine("Stock:" + stock_no + " Time:" + dt + " VOL" + mc[i].Groups[2] + " PRI" + mc[i].Groups[3]);
                    }
                }
            }

            if (fund > 0 && vol > 0)
            {
                return fund / vol;
            }
            else
            {
                return 0;
            }
        }

        public Dictionary<string, double[]> curDate() {
            string curDate = WebUtil.curDate(ConstDate.stocksStr);
            //string curDate = WebUtil.curDate("sh000001");
            Dictionary<string, double[]> curDic = new Dictionary<string, double[]>();
            string regexStr = "var\\shq_str_(s[h|z]\\d{6})=\"[\u4e00-\u9fa5_a-zA-Z]*,\\d*\\.\\d*,(\\d*\\.\\d*),(\\d*\\.\\d*),";
            //string regexStr = @"(\d{4}-\d{2}-\d{2})(\d*\.\d{2})";
            Regex regex = new Regex(regexStr, RegexOptions.Singleline | RegexOptions.Compiled);
            MatchCollection mc = regex.Matches(curDate);
            int count = mc.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    //LogUtil.LogLine(sub_stock_no + " " + date + " " + m.Groups[1] + " " + m.Groups[2]);
                    LogUtil.LogLine("Stock:" + mc[i].Groups[1] + " Yest" + mc[i].Groups[2] + " Cur" + mc[i].Groups[3]);
                    string y = Convert.ToString(mc[i].Groups[2]);
                    string c = Convert.ToString(mc[i].Groups[3]);
                    curDic.Add(Convert.ToString(mc[i].Groups[1]), new double[2] { Convert.ToDouble(y), Convert.ToDouble(c) });
                }
            }

            return curDic;

        }
    }
}
